import java.util.Stack;

/**
 * @author : zzr
 * @description :
 * @date :
 */
public class MySingleList {

    static class ListNode {
        public int val; //节点的value域
        public ListNode next; //下一个节点的地址

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head; //表示当前列表的头节点

    public void createList() {

        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }

    /**
     * 从头开始打印列表
     */
    public void display() {
        ListNode cur = head;

        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 从指定节点开始打印
     *
     * @param newHead
     */
    public void display(ListNode newHead) {
        ListNode cur = newHead;

        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;

        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        boolean flg = false;
        while (cur != null) {
            if (cur.val == key) {
                flg = true;
            }
            cur = cur.next;
        }
        return flg;
    }

    //头插法
    public void addFirst(int data) {

        ListNode listNode = new ListNode(data);

        listNode.next = head;
        head = listNode;
    }

    //尾插法
    public void addLast(int data) {

        ListNode listNode = new ListNode(data);

        ListNode cur = head;

        if (cur == null) {
            head = listNode;
            return;
        }

        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = listNode;
    }

    //任意位置插入，第一个是数据节点为0号下标
    public void addIndex(int index, int data) {

        //检查index合法性
        checkIndexForAddIndex(index);

        if (index == 0) {
            addFirst(data);
            return;
        }

        if (index == size()) {
            addLast(data);
            return;
        }

        ListNode cur = findIndexSubOne(index);
        ListNode listNode = new ListNode(data);

        listNode.next = cur.next;
        cur.next = listNode;

    }

    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        while ((index - 1) != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    private void checkIndexForAddIndex(int index) {
        if (index < 0 || index > size()) {
            throw new IndexIllegalException(index + "位置不合法");
        }
    }

    public void remove(int key) {

        if (head == null) {
            throw new EmptyLinkedListException("该链表为空，无法进行删除操作");
        }
        //单独删除头节点
        if (head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = searchPrev(key);
        if (cur == null) {
            System.out.println("没有要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;

    }

    private ListNode searchPrev(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除所有val == key的节点
    public void removeAllKey(int key) {

        //先判断链表是否为空
        if (head == null) {
            return;
        }

        ListNode cur = head.next;
        ListNode prev = head;

        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key) {
            head = head.next;
        }
    }

    //清空所有的链表
    public void clear() {
        this.head = null;
    }

    /**
     * 反转链表
     *
     * @return
     */
    public ListNode reverseList() {
        //空链表
        if (head == null)
            return null;
        //链表只有头节点
        if (head.next == null)
            return head;

        //从头节点的下一个节点开始反转
        ListNode cur = head.next;

        //反转之后，头节点一定成为了最后一个节点，其next一定是null
        head.next = null;

        while (cur != null) {
            //记录下当前反转节点的下一个节点
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    /**
     * 查找中间节点
     *
     * @return
     */
    public ListNode middleNode() {

        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next; //fast走两步
            slow = slow.next;      //slow走一步
        }
        return slow;
    }

    /**
     * 查找链表中倒数第K个节点
     *
     * @param k
     * @return
     */
    public ListNode findKthToTail(int k) {

        //判断k的合法性 同时检查是否为空链表
        if (k <= 0 || head == null) {
            return null;
        }

        ListNode fast = head;
        ListNode slow = head;

        while ((k - 1) != 0) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
            k--;
        }

        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    /**
     * 现有一链表的头指针pHead，给一定值x，编写一段代码将所有小于x的结点排在其余结点之前，
     * 且不能改变原来的数据顺序，返回重新排列后的链表的头指针。
     *
     * @param pHead
     * @param x
     * @return
     */
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;

        //cur != null 表示没有遍历完链表
        while (cur != null) {
            if (cur.val < x) {
                //在基准值左边，当第一次插入时
                if (bs == null) {
                    bs = be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                //在基准值右边，当第一次插入时
                if (as == null) {
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //如果基准值x较小，基准值左边没有节点
        if (bs == null) {
            return as;
        }
        be.next = as;

        //如果val最大的节点并非是最后一个节点
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    /**
     * 判断是否为回文链表
     *
     * @return
     */
    public boolean chkPalindrome() {

        //找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }


        //反转后半部分
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;//记录下来下一个节点
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }

        //从前 从后开始比较
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    /**
     * 判断链表是否为环
     *
     * @return
     */
    public boolean hasCycle() {

        //如果链表为空或者只有一个节点，肯定不存在环
        if (head == null || head.next == null) {
            return false;
        }

        //初始化快慢指针
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {

            slow = slow.next;
            fast = fast.next.next;

            //如果快慢指针相遇，则说明链表存在环
            if (slow == fast) {
                return true;
            }
        }
        return false;
    }

    public void createLoop() {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = head.next;
    }


    /**
     * 如果链表是环，返回环的入口点
     *
     * @return
     */
    public ListNode detectCycle() {
        //如果链表为空或者只有一个节点，肯定不存在环
        if (head == null || head.next == null) {
            return null;
        }

        //初始化快慢指针
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {

            slow = slow.next;
            fast = fast.next.next;

            //如果快慢指针相遇，则说明链表存在环
            if (slow == fast) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    /**
     * 删除链表中的重复节点
     *
     * @return
     */
    public ListNode deleteDuplication() {
        ListNode cur = head;
        ListNode newHead = new ListNode(-1);
        ListNode tmpHead = newHead;

        //遍历链表的每个节点
        while (cur != null) {
            if (cur.next != null && cur.val == cur.next.val) {
                //一直让cur走到不重复的节点，再把这个节点连接到新的不重复的链表中
                while (cur.next != null && cur.val == cur.next.val) {
                    cur = cur.next;
                }
                cur = cur.next;
            } else {
                tmpHead.next = cur;
                tmpHead = tmpHead.next;
                cur = cur.next;
            }
        }
        tmpHead.next = null;
        return newHead.next;
    }

    public void reservePrintList() {
        ListNode cur = head;
        Stack<ListNode> stack = new Stack<>();

        while (cur != null) {
            //将链表中的节点保存在栈中
            stack.push(cur);
            cur = cur.next;
        }

        while (!stack.isEmpty()) {
            //栈中的元素出栈
            ListNode top = stack.pop();
            System.out.print(top.val + " ");
        }
        System.out.println();
    }
}
